package co.fitstart.admin.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.demo2do.core.persistence.GenericDaoSupport;
import com.demo2do.core.support.Page;

import co.fitstart.admin.service.DistributionRecordService;
import co.fitstart.entity.user.distributor.DistributionRecord;
import co.fitstart.entity.user.distributor.DistributionRecordType;
import co.fitstart.entity.user.distributor.Distributor;

/**
 * 
 * @author mohanlan
 *  
 */
@Service("distributionRecordServiceImpl")
public class DistributionRecordServiceImpl implements DistributionRecordService {
    
    @Autowired
    private GenericDaoSupport genericDaoSupport;
    
    /*
     * (non-Javadoc)
     * @see co.fitstart.admin.service.DistributionRecordService#load(java.lang.Long)
     */
    public DistributionRecord load(Long id) {
        return genericDaoSupport.load(DistributionRecord.class, id);
    }
    
    /*
     * (non-Javadoc)
     * @see co.fitstart.admin.service.DistributionRecordService#listByDistributor(co.fitstart.entity.user.distributor.Distributor, com.demo2do.core.support.Page)
     */
    @SuppressWarnings("unchecked")
    public List<DistributionRecord> listByDistributor(Distributor distributor, Page page) {
        return genericDaoSupport.searchForList("FROM DistributionRecord WHERE distributor = :distributor AND disabled = false", "distributor", distributor);
    }

    /*
     * (non-Javadoc)
     * @see co.fitstart.admin.service.DistributionRecordService#listByType(co.fitstart.entity.user.distributor.DistributionRecordType, com.demo2do.core.support.Page)
     */
    @SuppressWarnings("unchecked")
    public List<DistributionRecord> listByType(DistributionRecordType type, Page page) {
        List<DistributionRecord> result = new ArrayList<>();
        
        if(type.isWithdrawed()) {
            result = genericDaoSupport.searchForList("FROM DistributionRecord WHERE type = :type AND disabled = false ORDER BY occurTime DESC", "type", type, page.getBeginIndex(), page.getMaxResultRecords());
            
        } else {
            List<DistributionRecordType> types = new ArrayList<>();
            types.add(DistributionRecordType.EARNED);
            types.add(DistributionRecordType.WITHDRAWING);
            
            result = genericDaoSupport.searchForList("FROM DistributionRecord WHERE type IN (:types) AND disabled = false ORDER BY occurTime DESC", "types", types, page.getBeginIndex(), page.getMaxResultRecords());
        }
        
        page.calculate(result.size());
        return result.size() > page.getEveryPage() ? result.subList(0, page.getEveryPage()) : result;
    }
    
    /*
     * (non-Javadoc)
     * @see co.fitstart.admin.service.DistributionRecordService#listByTime(co.fitstart.entity.user.distributor.DistributionRecordType, java.util.Date, java.util.Date)
     */
    @SuppressWarnings("unchecked")
    public List<DistributionRecord> listByTime(DistributionRecordType type, Date beginTime, Date endTime) {
        
        List<DistributionRecord> result = new ArrayList<DistributionRecord>();
        
        Map<String, Object> parameters = new HashMap<String, Object>();
        
        parameters.put("beginTime", beginTime);
        parameters.put("endTime", endTime);
        
        if(type.isWithdrawing()) {
            List<DistributionRecordType> types = new ArrayList<>();
            types.add(DistributionRecordType.EARNED);
            types.add(DistributionRecordType.WITHDRAWING);
            
            parameters.put("types", types);
            
            result = genericDaoSupport.searchForList("FROM DistributionRecord WHERE disabled = false AND type IN (:types) AND occurTime BETWEEN :beginTime AND :endTime ORDER BY occurTime DESC", parameters);
        } else {
            parameters.put("type", type);
            
            result = genericDaoSupport.searchForList("FROM DistributionRecord WHERE disabled = false AND type = :type AND payTime BETWEEN :beginTime AND :endTime ORDER BY payTime DESC", parameters);
        }
        
        return result;
    }
}
